<?php

class FilescanController extends MyClass_Action {
	protected $controller;
	protected $action;
	protected $_user;
	private $groupdbAdapter;//数据库连接
	
	public function init() {
		$this->commonAction();
		//连接伊顿数据库
		//$this->groupdbAdapter=$this->getDbLink(1);
	}
	//用户首页
	public function indexAction() {
		try {
			$uid_class = $this->_request->getParam('campus');//获取校区id
			//print_r($uid_class);exit;
			//添加where条件
			$where = '';
			$where = $uid_class ? " and uid_class='{$uid_class}'" : '';
			//获取当前页面 并进行判断
			$curpage = $this->_request->getParam('page');
			$curpage = $curpage < 1 ? 1 : $curpage;
			$count = 20;
			//总页数
			$num = $this->_dbAdapter->fetchOne ( "select count(fileid) from samba_files where isdelete=0 and isignore=0 and isover=0 {$where}" );
			$number = round($num/$count);//总页数
			//当当前页大于总页数时，当前页就等于总页数
			$curpage = ($curpage > $number) && ($number != 0) ? $number : $curpage;
			
			//获取校区信息
			$schoolinfo = $this->getschoolinfo();
			//取得当前页对应的数据条数
			$offset = ($curpage - 1) * $count;
			//查询共享文件夹20条数据
			$sql = "select * from samba_files 
			where isignore=0 and isover=0 and isdelete=0 {$where} 
			order by createdatetime desc 
			limit {$offset}, {$count}";
			$res = $this->_dbAdapter->fetchAll($sql);
			
			//添加校区名称
			foreach($res as $key => $value) {
				$res[$key]['filepath'] = $this->getfileaddress($value['filepath']);
				$res[$key]['schoolname'] = $schoolinfo[$value['uid_class']];
			}
			//分页操作
			$param ['file'] = '/' . $this->controller . '/' . $this->action;
			$param ['totalnum'] = $num;
			$param ['perpagenum'] = $count; // 每页显示的数目
			$param ['disnum'] = 11; // 取单数显示，当前页停在中间
			$param ['curpage'] = $curpage; // 当前页码
			$this->view->allnum= $param ['totalnum'];
			$page = new MyClass_Page ( $param );
			//设置分页传递参数
			$page->setvar ( array ('campus' => $uid_class) );
			$this->view->page= $page->getNumPage ();
			
			//设置模板
			$this->view->curpage = $curpage;//当前页
			$this->view->uid_class = $uid_class;//校区id
			$this->view->schoolinfo = $schoolinfo;//校区信息
			$this->view->fileinfo = $res;//文件信息
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), '注意', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//操作文件
	public function ignorefileAction() {
		if(isset($_POST['submit'])) {
			//提交数据
			$post = $this->_request->getpost();
			//获取需要修改的文件id
			$fileids = array();
			foreach($post['fileid'] as $key => $value) {
				$fileids[] = $key;
			}
			//判断需要改变的字段
			$field = $post['operate'] == 'ignore' ? 'isignore' : 'isover';
			
			$res = $this->_dbAdapter->update('samba_files', array('' . $field . ''=>1), 'fileid in (' . implode(',', $fileids) . ')');
			if($res) {
				$this->feedback ('操作成功', '成功', '/filescan', 'tip' );
			} else {
				$this->feedback ('操作失败', '注意', 'javascript:window.history.back();', 'warning' );
			}
		} else {
			//获取文件id
			$fileid = $this->_request->getParam('fileid');
			//操作类型 忽略操作与完成操作
			$operate = $this->_request->getParam('operate');
			$uid_class = $this->_request->getParam('campus');//校区id
			$curpage = $this->_request->getParam('page');//当前页
			$field = $operate == 'ignore' ? 'isignore' : 'isover';
			//改变状态
			$res = $this->_dbAdapter->update('samba_files', array('' . $field . ''=>1), 'fileid='.$fileid);
			
			if($res) {
				$this->feedback ('操作成功', '成功', '/filescan/index/campus/'.$uid_class.'/page/'.$curpage, 'tip' );
			} else {
				$this->feedback ('操作失败', '注意', 'javascript:window.history.back();', 'warning' );
			}
		}
	}
	
	//已忽略文件
	public function ignoredAction() {
		$curpage = $this->_request->getParam('page');
		$curpage = $curpage < 1 ? 1 : $curpage;
		$count = 20;
		//获取校区信息
		$schoolinfo = $this->getschoolinfo();
		
		$offset = ($curpage - 1) * $count;
		//查询共享文件夹20条数据
		$sql = "select * from samba_files where isignore=1 order by createdatetime desc limit {$offset}, {$count}";
		$res = $this->_dbAdapter->fetchAll($sql);
		//添加校区名称
		foreach($res as $key => $value) {
			$res[$key]['schoolname'] = $schoolinfo[$value['uid_class']];
		}
		
		$param ['file'] = '/' . $this->controller . '/' . $this->action;
		$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'select count(fileid) from samba_files where isignore=1' ); 
		$param ['perpagenum'] = $count; // 每页显示的数目
		$param ['disnum'] = 11; // 取单数显示，当前页停在中间
		$param ['curpage'] = $curpage; // 当前页码
		$this->view->allnum= $param ['totalnum'];
		$page = new MyClass_Page ( $param );
		$this->view->page= $page->getNumPage ();
		
		//设置模板
		$this->view->fileinfo = $res;
	}
	
	//已删除文件
	public function deleteAction() {
		$curpage = $this->_request->getParam('page');
		$curpage = $curpage < 1 ? 1 : $curpage;
		$count = 20;
		//获取校区信息
		$schoolinfo = $this->getschoolinfo();
		
		$offset = ($curpage - 1) * $count;
		//查询共享文件夹20条数据
		$sql = "select * from samba_files where isdelete=1 order by createdatetime desc limit {$offset}, {$count}";
		$res = $this->_dbAdapter->fetchAll($sql);
		//添加校区名称
		foreach($res as $key => $value) {
			$res[$key]['schoolname'] = $schoolinfo[$value['uid_class']];
		}
		
		$param ['file'] = '/' . $this->controller . '/' . $this->action;
		$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'select count(fileid) from samba_files where isignore=1' ); 
		$param ['perpagenum'] = $count; // 每页显示的数目
		$param ['disnum'] = 11; // 取单数显示，当前页停在中间
		$param ['curpage'] = $curpage; // 当前页码
		$this->view->allnum= $param ['totalnum'];
		$page = new MyClass_Page ( $param );
		$this->view->page= $page->getNumPage ();
		
		//设置模板
		$this->view->fileinfo = $res;
	}
	
	//已完成文件
	public function isoveredAction() {
		$curpage = $this->_request->getParam('page');
		$curpage = $curpage < 1 ? 1 : $curpage;
		$count = 20;
		//获取校区信息
		$schoolinfo = $this->getschoolinfo();
		
		$offset = ($curpage - 1) * $count;
		//查询共享文件夹20条数据
		$sql = "select * from samba_files where isover=1 order by createdatetime desc limit {$offset}, {$count}";
		$res = $this->_dbAdapter->fetchAll($sql);
		//添加校区名称
		foreach($res as $key => $value) {
			$res[$key]['schoolname'] = $schoolinfo[$value['uid_class']];
		}
		
		$param ['file'] = '/' . $this->controller . '/' . $this->action;
		$param ['totalnum'] = $this->_dbAdapter->fetchOne ( 'select count(fileid) from samba_files where isover=1' ); 
		$param ['perpagenum'] = $count; // 每页显示的数目
		$param ['disnum'] = 11; // 取单数显示，当前页停在中间
		$param ['curpage'] = $curpage; // 当前页码
		$this->view->allnum= $param ['totalnum'];
		$page = new MyClass_Page ( $param );
		$this->view->page= $page->getNumPage ();
		
		//设置模板
		$this->view->fileinfo = $res;
	}
	
	//获取校区信息
	public function getschoolinfo() {
		//获取所有校区信息
		$res = $this->_dbAdapter->fetchAll('select schoolid,schoolname from school');
		//重组数组，以便于程序获取信息
		$schoolinfo = array();
		foreach($res as $key => $value) {
			$schoolinfo[$value['schoolid']] = $value['schoolname'];
		}
		return $schoolinfo;
	}
	
	//获取文件下载地址
	public function getfileaddress($filepath) {
		$path = "http://192.168.1.2/samba/" . substr(mb_convert_encoding($filepath, "GB2312", "UTF-8"), 13) . "";
		
		$array = explode('/', $path);
		$codepath = '';
		$count = count($array);
		foreach($array as $key => $value) {
			if($key < 1) {
				$codepath .= $value . '/';
			} else {
				if(($key+1) == $count) {
					$codepath .= urlencode($value);
				} else {
					$codepath .= urlencode($value) . '/';
				}
			}
		}
		return $codepath;
	}
	
	//以下内容保持不变
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		throw new Exception ( 'Invalid method' );
	}
}

